package com.kp.fintech.oauth.controller;

import com.alibaba.fastjson.JSONObject;
import com.kp.fintech.oauth.vo.UserLoginBean;
import com.kp.fintech.oauth.service.OauthTokenService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/oauth")
public class OauthTokenController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private OauthTokenService oauthTokenService;
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private LoadBalancerClient loadBalancer;

    @RequestMapping(value = "/oauth-token", method = RequestMethod.POST)
    public String authToken(@RequestBody Map<String, Object> uriParam) {

        LinkedHashMap linkedHashMap_LoginBean = (LinkedHashMap) uriParam.getOrDefault("loginBean", null);
        String json_LoginBean = new JSONObject().toJSONString(linkedHashMap_LoginBean);
        UserLoginBean uriLoginBean = new JSONObject().parseObject(json_LoginBean, UserLoginBean.class);

        return oauthTokenService.getTockenNo(uriLoginBean);
    }

    @RequestMapping(value = "/validate-token", method = RequestMethod.POST)
    public Boolean validateToken(@RequestBody Map<String, String> uriParam) {
        return oauthTokenService.validateToken(uriParam.get("tokenNo"),uriParam.get("userName"));
    }
}